package com.itheima.domain;

import com.mysql.fabric.xmlrpc.base.Data;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;

@Component
@Aspect
public class FieldFillAdvice {
    @Pointcut("execution(* com.itheima.service.EquipmentService.add*(..))")
    public void show(){}
    @Around("show()")
    public Object fillField(ProceedingJoinPoint pj)throws Throwable{
//        获取方法的标签
        Signature signature = pj.getSignature();
        if(signature instanceof MethodSignature){
     MethodSignature ms= (MethodSignature) signature;
////     获取方法名称
//            String name = ms.getName();
//            获取方法的参数类型
            Class parameterTypes = ms.getParameterTypes()[0];
//            获取域
            Field created = parameterTypes.getDeclaredField("created");
            Field updated = parameterTypes.getDeclaredField("updated");
            created.setAccessible(true);
            updated.setAccessible(true);
            Object args = pj.getArgs()[0];
            Object created01 = created.get(args);
            Object updated01 = updated.get(args);
            if (created01==null){
                created.set(args,new Date());
            }
            if (updated01==null){
               updated.set(args,new Date());
            }

        }

        return pj.proceed(pj.getArgs());
    }
}
